package com.yx.outSourcing.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.outSourcing.R.Result;
import com.yx.outSourcing.entity.dto.AccessApplicationDTO;
import com.yx.outSourcing.entity.vo.AccessApplicationVO;
import com.yx.outSourcing.entity.vo.AccessApplicationDetailVO;
import com.yx.outSourcing.service.IAccessApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 临时通行证申请单表 前端控制器
 * </p>
 *
 * @author zcc
 * @since 2025-07-29
 */
@Api(tags = "申请单管理接口")
@RestController
@RequestMapping("/api/apply")
public class AccessApplicationController {

    @Autowired
    private IAccessApplicationService accessApplicationService;

    /**
     * 临时通行证申请表查询
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("长协人员临时通行证申请")
    public Result<Page<AccessApplicationVO>> appList(){
        return Result.success(accessApplicationService.appList());
    }

    /**
     * 新增申请单
     * @return
     */
    @PostMapping
    @ApiOperation("新增申请单")
    public Result<Long> create(@Valid @RequestBody AccessApplicationDTO dto) {
        return Result.success(accessApplicationService.createApplication(dto));
    }

    /**
     * 修改申请单
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation("修改申请单")
    public Result<Void> update(@PathVariable Long id,
                               @Valid @RequestBody AccessApplicationDTO dto) {
        accessApplicationService.updateApplication(id, dto);
        return Result.success();
    }

    /**
     * 删除申请单
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除申请单(数据会转移到归档表)")
    public Result<Void> delete(@PathVariable Long id) {
        accessApplicationService.deleteApplication(id);
        return Result.success();
    }

    /**
     * 获取申请单详情
     */
    @GetMapping("/{id}")
    @ApiOperation("获取申请单详情")
    public Result<AccessApplicationDetailVO> getDetail(@PathVariable Long id) {
        AccessApplicationDetailVO detail = accessApplicationService.getApplicationDetail(id);
        return Result.success(detail);
    }
}
